using System;
using System.Collections.Generic;
using System.Text;
using MacomberMap.Common.Database_Interface.CIM.Components;
using System.Windows.Forms;
using System.Data;
using System.Drawing;

namespace MacomberMap.Loader.Export.NOMCRs
{
    /// <summary>
    /// This class holds an OTS rule exception
    /// </summary>
    public class MM_OTS_Rule_Exception
    {
        #region Variable declarations
        /// <summary>The rule on which the exception was noted</summary>
        public MM_OTS_Rule BaseRule;

        /// <summary>The element that failed the exception</summary>
        public CIM_Element ExceptionElement;

        /// <summary>The UID that resolves the issue</summary>
        public int UID = -1;

        /// <summary>The node associated with the execption</summary>
        public TreeNode AssociatedNode;

        /// <summary>The data row (if any) associated with the solution</summary>
        public DataRow NOMCR;

        /// <summary>Whether our NOMCR resolves the issue</summary>
        public bool Resolved = false;
        #endregion

        /// <summary>
        /// The exception
        /// </summary>
        /// <param name="BaseRule"></param>
        /// <param name="ExceptionElement"></param>
        /// <param name="NOMCRs"></param>
        /// <param name="tvExceptions"></param>
        public MM_OTS_Rule_Exception(MM_OTS_Rule BaseRule, CIM_Element ExceptionElement, TreeView tvExceptions, DataTable NOMCRs)
        {
            this.BaseRule = BaseRule;
            this.ExceptionElement = ExceptionElement;

            DataRow[] FoundNOMCR = NOMCRs.Select("rdfID='" + ExceptionElement.rdfID.ToString() + "'");
            if (FoundNOMCR.Length == 1)
                if (BaseRule.TestNOMCR(FoundNOMCR[0],this))
                    BaseRule.ConfirmNOMCR(this.NOMCR = FoundNOMCR[0]);


            tvExceptions.BeginInvoke(new SafeAssignNode(AssignNode), BaseRule.BaseNode.Nodes);            
        }
        
        private delegate void SafeAssignNode(TreeNodeCollection Nodes);
        
        
        /// <summary>
        /// Assign and create a new node
        /// </summary>
        /// <param name="Nodes"></param>
        private void AssignNode(TreeNodeCollection Nodes)
        {                        
            if (ExceptionElement.Substation == null && ExceptionElement.VoltageLevel == null)
                this.AssociatedNode = Nodes.Add(ExceptionElement.Type.Split(':')[1] + " " + ExceptionElement.Name + " (" + ExceptionElement.TEID + ")");
            else if (ExceptionElement.Substation == null)
                this.AssociatedNode = Nodes.Add(ExceptionElement.VoltageLevel.Name + " " + ExceptionElement.Type.Split(':')[1] + " " + ExceptionElement.Name + " (" + ExceptionElement.TEID + ")");
            else if (ExceptionElement.VoltageLevel == null)
                this.AssociatedNode = Nodes.Add(ExceptionElement.Type.Split(':')[1] + " " + ExceptionElement.Substation.Name + " " + ExceptionElement.Name + " (" + ExceptionElement.TEID + ")");
            else
                this.AssociatedNode = Nodes.Add(ExceptionElement.VoltageLevel.Name + " " + ExceptionElement.Type.Split(':')[1] + " " + ExceptionElement.Substation.Name + " " + ExceptionElement.Name + " (" + ExceptionElement.TEID + ")");

            if (NOMCR == null)
                this.AssociatedNode.ForeColor = Color.Red;
            else
                this.AssociatedNode.ForeColor = Color.Green;
                    
            this.AssociatedNode.Tag = this;



            
        }

        /// <summary>
        /// Assign a data row (NOMCR) to the solution
        /// </summary>
        /// <param name="AssignedRow"></param>
        public void AssignRow(DataRow AssignedRow)
        {
            this.NOMCR = AssignedRow;
            this.AssociatedNode.ForeColor = Color.Green; 
        }
    }
}
